package com.chengdu.third.controller.admin;

import com.chengdu.third.pojo.Laws;
import com.chengdu.third.service.ILawsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/admin")
public class ZCFGController {

    @Autowired
    private ILawsService lawsService;


    //查询所有分类的方法
    @GetMapping("/getLawsList")
    public String getLawsList(@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum, Model model){
        //规定第几页，每页多少个
//        pagenum是统计页数的
        PageHelper.startPage(pagenum, 5);
        List<Laws> lawsList = lawsService.findAll();
        //得到分页结果对象
        PageInfo<Laws> pageInfo = new PageInfo<>(lawsList);
        //根据pageInfo来展示出数据
        model.addAttribute("pageInfo", pageInfo);
        return "admin/Laws_list";
    }


    //跳转新增政策法规的页面
    @GetMapping("/getLawsList/add")
    public String add(Model model){
        model.addAttribute("laws", new Laws());  //返回一个blog对象给前端th:object
        return "admin/Laws_add";
    }

    @PostMapping("/getLawsList")
    public String addToLawsList(Laws laws, HttpSession session, RedirectAttributes attributes){
        laws.setCreate_time(new Timestamp(new Date().getTime()));
        attributes.addFlashAttribute("msg", "添加成功");
        lawsService.addlaws(laws);
        return "redirect:/admin/getLawsList";
    }


    //删除政策法规
    @GetMapping("/lawsDelete/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes){
        lawsService.deleteByid(id);
        attributes.addFlashAttribute("msg", "删除成功");
        return "redirect:/admin/getLawsList";
    }

    //查看政策法规具体内容
    @GetMapping("/lawsShow/{id}/show")
    public String show(@PathVariable Long id,Model model){
        Laws laws = lawsService.getLawsById(id);
        model.addAttribute("laws",laws);
        return "admin/Laws_show";
    }

    //修改信息显示
    @GetMapping("/lawsUpdate/{id}/update")
    public String lawsUpdate(@PathVariable Long id, Model model){
        model.addAttribute("laws", lawsService.getLawsById(id));
         return "admin/Laws_update";
    }

    //真正修改提交的页面
    @PostMapping("/lawsUpdateSubmit/{id}")
    public String lawsUpdateSubmit(@PathVariable Long id, Laws laws, RedirectAttributes attributes){  //修改
        laws.setId(id);
        laws.setUpdate_time(new Timestamp(new Date().getTime()));
        lawsService.updateLawsById(laws);
        attributes.addFlashAttribute("msg", "修改成功");
        return "redirect:/admin/getLawsList";
    }



}
